﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Business.Compta;
using DAO.Compta;

namespace Compta
{
    /// <summary>
    /// Interaction logic for UsersUI.xaml
    /// </summary>
    public partial class UsersUI : UserControl
    {
        private ObservableCollection<User> _users;

        public ObservableCollection<User> Users
        {
            get
            {
                if (_users == null)
                {
                    _users= new ObservableCollection<User>(ComptaUsersDAO.getAllUsers());
                }
                return _users;
            }
        }

        public UsersUI()
        {
            InitializeComponent();
        }



        private void validateUser(object sender, DevExpress.Wpf.Grid.GridRowValidationEventArgs e)
        {
            User line = (User)e.Row;
            if (line != null)
            {
                if (line.Id == -1)
                {
                    try
                    {
                        line.Id = ComptaUsersDAO.InserNewUser(line);
                        line.commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error while modifying or insert compta line ! (id = " + line.Id + ")\n" + ex, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                        _users.Remove(line);
                    }
                }
                else
                {
                    try
                    {
                        ComptaUsersDAO.ModifyUser(line);
                        line.commit();
                    }
                    catch (Exception ex)
                    {
                        line.reject();
                        MessageBox.Show("error while modifying or insert compta line ! (id = " + line.Id + ")\n" + ex, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }

        }

        private void keyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                DevExpress.Wpf.Grid.TableView view = (DevExpress.Wpf.Grid.TableView)sender;
                if (view.ActiveEditor == null || !view.ActiveEditor.IsEditorActive)
                {
                    int listIndex = this.gridControl1.GetRowListIndex(view.FocusedRowHandle);
                    ObservableCollection<User> list =
                        (ObservableCollection<User>)this.gridControl1.DataSource;
                    if (listIndex >= 0)
                    {
                        User line = list[listIndex];
                        try
                        {
                            int _return = ComptaUsersDAO.deleteUser(line);

                            if (_return == 1)
                            {
                                list.RemoveAt(listIndex);
                            }
                            else
                            {
                                MessageBox.Show("L'utilisateur n'a pas été supprimé. Voir Mamour !", "ERROR !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("error while deleting user line (id = " + line.Id + ")\n" + ex.StackTrace, "ERROR !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
        }
    }

    
}
